// <copyright file="SSISWrapperTest.cs" company="Bill Fellows">
// Copyright (c) But not really, use it as you see fit. All rights reserved.
// </copyright>
namespace SSISDeploy.Components.Tests
{
    using System;
    using System.Collections.Generic;
    using System.Text;

    using SSISDeploy.Components;
    using NUnit.Framework;

    /// <summary>
    /// This will test the wrapper I've put around the MS object library.
    /// </summary>
    [TestFixture]
    public class SSISWrapperTest
    {
        /// <summary>
        /// Our reference instance
        /// </summary>
        private SSISDeploy.Components.SSISWrapper wrapper;

        /// <summary>
        /// The SQL Server we are testing against
        /// </summary>
        private string server;

        /// <summary>
        /// A reference to the base location in sql server
        /// </summary>
        private string rootSqlFolder;

        /// <summary>
        /// A *hopefully* unique folder name within SQL Server.
        /// </summary>
        private string uniqueSQLFolderName;

        /// <summary>
        /// The name cannot contain any of the following characters: / \ : [ ] . =
        /// </summary>
        private string invalidSQLFolderName;

        /// <summary>
        /// An example of a folder name that is too long
        /// </summary>
        private string tooLongSQLFolderName;

        /// <summary>
        /// The unique path that will be used for testing recursive folder creation
        /// </summary>
        private string recursivePath;

        /// <summary>
        /// The parent folder for the recursive test
        /// </summary>
        private string recursiveRoot;

        /// <summary>
        /// Path and filename of a package
        /// </summary>
        private string localPackage;

        /// <summary>
        /// Name of the package on the server
        /// </summary>
        private string storePackage;

        /// <summary>
        /// The also hopefully unique folder name for testing the folder renaming ability
        /// </summary>
        private string renameFolder;

        /// <summary>
        /// Initialze our fixture with whatever we need to do
        /// </summary>
        [TestFixtureSetUp]
        public void Init()
        {
            string path = string.Empty;
            path = @"\WaterWaterEverywhere";
            this.wrapper = new SSISWrapper();
            this.invalidSQLFolderName = @"The name cannot contain any of the following characters: / \ : [ ] . =";
            this.localPackage = @"..\..\HelloWorld.dtsx";
            this.recursivePath = path + @"\Not\A\Drop\To\Drink";
            this.recursiveRoot = path;
            this.renameFolder = @"TheEagleNeverLostSoMuchTimeAsWhenHeSubmittedToLearnOfTheCrow";
            this.rootSqlFolder = @"\\";
            this.server = "localhost";
            this.storePackage = "TooMuchOrNotEnough";
            this.tooLongSQLFolderName = "IAmTheTeacherOfAthletes,HeThatByMeSpreadsAWiderBreastThanMyOwnProvesTheWidthOfMyOwn,HeMostHonorsMyStyleWhoLearnsUnderItToDestroyTheTeacher";
            this.uniqueSQLFolderName = "MyfingersEmitsparksoffirewithExpectationsofmyfuturelabours";
        }

        /// <summary>
        /// Ensure that the bogus folder(s) we created are removed
        /// </summary>
        [TestFixtureTearDown]
        public void Cleanup()
        {
            //if (this.wrapper.FolderExists(this.server, this.rootSqlFolder, this.uniqueSQLFolderName))
            //{
            //    this.wrapper.DeleteFolder(this.server, this.rootSqlFolder, this.uniqueSQLFolderName);
            //}

            //if (this.wrapper.FolderExists(this.server, this.recursiveRoot))
            //{
            //    this.wrapper.DeleteFolderRecursive(this.server, this.recursiveRoot);
            //    this.wrapper.DeleteFolder(this.server, this.rootSqlFolder, this.recursiveRoot);
            //}
        }
        
        /// <summary>
        /// Validate the behaviour of the FolderExists method
        /// </summary>
        [Test]
        public void FolderExists()
        {
            Assert.IsTrue(this.wrapper.FolderExists(this.server, this.rootSqlFolder));
        }
        /*
        /// <summary>
        /// Validate the behaviour of the FolderExists method.  If this fails, then either there
        /// is a SQL Server issue (doesn't exist, can't connect etc) or I borked the FolderExists code
        /// 
        /// 2008-07-17 Why I am testing this twice with the same parameters is an excellent question
        /// that I have no answer for
        /// </summary>
        [Test]
        public void FolderExistsRootFolder()
        {
            Assert.IsTrue(this.wrapper.FolderExists(this.server, this.rootSqlFolder));
        }

        /// <summary>
        /// I keep using the FolderExists method incorrectly as I neglect to build the path
        /// so I am going to make a method that takes a parent path and folder name
        /// and handle the path building in the method
        /// </summary>
        [Test]
        public void FolderExistsByParent()
        {
            // I am less than thrilled with this method as it doesn't give a positive 
            // test.
            Assert.AreEqual(this.wrapper.FolderExists(this.server, this.rootSqlFolder, this.uniqueSQLFolderName), this.wrapper.FolderExists(this.server, this.rootSqlFolder + this.uniqueSQLFolderName));
        }
        */

        /// <summary>
        /// Attempt to create a folder and an invalid folder in SQL Server
        /// </summary>
        [Test]
        public void CreateFolder()
        {
            Assert.IsTrue(this.wrapper.CreateFolder(this.server, this.rootSqlFolder, this.uniqueSQLFolderName));
            Assert.IsFalse(this.wrapper.CreateFolder(this.server, this.rootSqlFolder, this.invalidSQLFolderName));
            Assert.IsFalse(this.wrapper.CreateFolder(this.server, this.rootSqlFolder, this.tooLongSQLFolderName));
        }

        /// <summary>
        /// Delete our unique folder, if it exists.  This depends on the folder exists code not being broken
        /// </summary>
        [Test]
        public void DeleteFolder()
        {
            Assert.IsTrue(this.wrapper.DeleteFolder(this.server, this.rootSqlFolder, this.uniqueSQLFolderName));
        }

        /// <summary>
        /// Test the recursive folder creation functionality
        /// </summary>
        [Test]
        public void CreateFolderRecursive()
        {
            Assert.IsTrue(this.wrapper.CreateFoldersRecursive(this.server, this.recursivePath));
        }

        /// <summary>
        /// Test the recursive folder deletion functionality
        /// </summary>
        [Test]
        public void DeleteFolderRecursive()
        {
            Assert.IsTrue(this.wrapper.DeleteFolderRecursive(this.server, this.recursiveRoot));
        }

        /*
        /// <summary>
        /// Test for the creation of a package
        /// </summary>
        [Test]
        public void CopyPackage()
        {
            Assert.IsTrue(this.wrapper.CopyPackage(this.server, this.localPackage, string.Format("{0}\\{1}", this.rootSqlFolder, this.storePackage)));
        }

        /// <summary>
        /// Test for handling removal of packages
        /// </summary>
        [Test]
        public void DeletePackage()
        {
            Assert.IsTrue(this.wrapper.DeletePackage(this.server, this.rootSqlFolder, this.storePackage));
        }

        /// <summary>
        /// Query SQL Server to see if a package exists.  The name is a dirty hack as it will get 
        /// nunit to evaluate it in a favorable order.  My cursory reading indicates that I am testing
        /// wrong as there doesn't appear to be an ability to say test this only if test X succeeds.
        /// I should have a setup/teardown for these scnearios.  That's a TODO
        /// </summary>
        [Test]
        public void CursePackageExistance()
        {
            Assert.IsTrue(this.wrapper.PackageExists(this.server, string.Format("{0}\\{1}", this.rootSqlFolder, this.storePackage)));
        }

        /// <summary>
        /// Test the ability to rename an existing folder
        /// </summary>
        [Test]
        public void Rename()
        {
            if (!this.wrapper.FolderExists(this.server, this.uniqueSQLFolderName))
            {
                Assert.IsTrue(this.wrapper.CreateFolder(this.server, this.rootSqlFolder, this.uniqueSQLFolderName));
            }

            if (this.wrapper.FolderExists(this.server, this.renameFolder))
            {
                Assert.IsTrue(this.wrapper.DeleteFolder(this.server, this.rootSqlFolder, this.renameFolder));
            }

            if (this.wrapper.FolderExists(this.server, this.rootSqlFolder, this.uniqueSQLFolderName) && !this.wrapper.FolderExists(this.server, this.rootSqlFolder, this.renameFolder))
            {
                Assert.IsTrue(this.wrapper.RenameFolder(this.server, this.rootSqlFolder, this.uniqueSQLFolderName, this.renameFolder));
                Assert.IsTrue(this.wrapper.FolderExists(this.server, this.rootSqlFolder, this.renameFolder));
                Assert.IsTrue(this.wrapper.RenameFolder(this.server, this.rootSqlFolder, this.renameFolder, this.uniqueSQLFolderName));
                Assert.IsTrue(this.wrapper.FolderExists(this.server, this.rootSqlFolder, this.uniqueSQLFolderName));
            }
            else
            {
                Assert.Fail("Test not in the correct state");
            }
        }
        */
    }
}
